<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>业务知识管理配置</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <style>
        :root {
            --primary-color: #1890ff;
            --secondary-color: #52c41a;
            --background-color: #f5f8fa;
            --card-bg: #ffffff;
            --text-color: #333333;
            --border-color: #e8e8e8;
            --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            --radius: 8px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: var(--background-color);
            color: var(--text-color);
            line-height: 1.6;
        }

        .header {
            background-color: var(--card-bg);
            padding: 1rem;
            box-shadow: var(--shadow);
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .container {
            max-width: 100%;
            margin: 0 auto;
            padding: 1rem 2rem;
        }

        .title {
            font-size: 1.6rem;
            font-weight: 600;
            color: var(--primary-color);
            margin-bottom: 0.3rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .subtitle {
            font-size: 0.95rem;
            color: #666;
            margin-bottom: 0.8rem;
        }

        .toolbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1rem;
            gap: 1rem;
        }

        .search-container {
            display: flex;
            gap: 0.5rem;
            flex: 1;
            max-width: 400px;
        }

        .search-input {
            flex: 1;
            padding: 0.75rem 1rem;
            font-size: 1rem;
            border: 1px solid var(--border-color);
            border-radius: var(--radius);
            transition: all 0.3s;
            outline: none;
        }

        .search-input:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
        }

        .btn {
            padding: 0.75rem 1.5rem;
            border: none;
            border-radius: var(--radius);
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            text-decoration: none;
        }

        .btn-primary {
            background-color: var(--primary-color);
            color: white;
        }

        .btn-primary:hover {
            background-color: #40a9ff;
        }

        .btn-success {
            background-color: var(--secondary-color);
            color: white;
        }

        .btn-success:hover {
            background-color: #73d13d;
        }

        .btn-danger {
            background-color: #ff4d4f;
            color: white;
        }

        .btn-danger:hover {
            background-color: #ff7875;
        }

        .btn-secondary {
            background-color: #f5f5f5;
            color: #666;
            border: 1px solid var(--border-color);
        }

        .btn-secondary:hover {
            background-color: #e6e6e6;
        }

        .card {
            background-color: var(--card-bg);
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            overflow: hidden;
        }

        .table-container {
            overflow-x: auto;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
        }

        .table th,
        .table td {
            padding: 1rem;
            text-align: left;
            border-bottom: 1px solid var(--border-color);
        }

        .table th:last-child,
        .table td:last-child {
            width: 200px;
            min-width: 200px;
        }

        .table th {
            background-color: #fafafa;
            font-weight: 500;
            color: #666;
        }

        .table tr:hover {
            background-color: #f9f9f9;
        }

        .badge {
            display: inline-block;
            padding: 0.25rem 0.5rem;
            font-size: 0.75rem;
            font-weight: 500;
            border-radius: 20px;
        }

        .badge-success {
            background-color: #f6ffed;
            color: var(--secondary-color);
        }

        .badge-secondary {
            background-color: #f5f5f5;
            color: #666;
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
        }

        .modal.show {
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .modal-content {
            background-color: var(--card-bg);
            border-radius: var(--radius);
            width: 90%;
            max-width: 600px;
            max-height: 90vh;
            overflow-y: auto;
        }

        .modal-header {
            padding: 1.5rem;
            border-bottom: 1px solid var(--border-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .modal-title {
            font-size: 1.2rem;
            font-weight: 600;
        }

        .modal-body {
            padding: 1.5rem;
        }

        .modal-footer {
            padding: 1rem 1.5rem;
            border-top: 1px solid var(--border-color);
            display: flex;
            justify-content: flex-end;
            gap: 0.5rem;
        }

        .form-group {
            margin-bottom: 1rem;
        }

        .form-label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
        }

        .form-control {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid var(--border-color);
            border-radius: var(--radius);
            font-size: 1rem;
            transition: all 0.3s;
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
        }

        .form-control[type="checkbox"] {
            width: auto;
            margin-right: 0.5rem;
        }

        .checkbox-group {
            display: flex;
            align-items: center;
        }

        .close-btn {
            background: none;
            border: none;
            font-size: 1.5rem;
            cursor: pointer;
            color: #999;
        }

        .close-btn:hover {
            color: #333;
        }

        .empty-state {
            text-align: center;
            padding: 3rem;
            color: #999;
        }

        .empty-icon {
            font-size: 3rem;
            margin-bottom: 1rem;
            color: #ccc;
        }

        .loading {
            text-align: center;
            padding: 2rem;
            color: #666;
        }

        .spinner {
            width: 20px;
            height: 20px;
            border: 2px solid rgba(0, 0, 0, 0.1);
            border-top-color: var(--primary-color);
            border-radius: 50%;
            animation: spin 1s linear infinite;
            display: inline-block;
            margin-right: 0.5rem;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .action-buttons {
            display: flex;
            gap: 0.5rem;
            justify-content: center;
            flex-wrap: nowrap;
            flex-direction: row;
        }

        .btn-sm {
            padding: 0.375rem 0.75rem;
            font-size: 0.875rem;
            border-radius: 4px;
            min-width: 60px;
            white-space: nowrap;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 0.25rem;
            flex-shrink: 0;
        }

        .nav-links {
            display: flex;
            gap: 0.8rem;
            margin-bottom: 0.5rem;
        }

        .nav-link {
            padding: 0.4rem 0.8rem;
            text-decoration: none;
            color: #666;
            border-radius: var(--radius);
            transition: all 0.3s;
            font-size: 0.95rem;
        }

        .nav-link:hover {
            background-color: #f0f5ff;
            color: var(--primary-color);
        }

        .nav-link.active {
            background-color: var(--primary-color);
            color: white;
        }

        @media (max-width: 768px) {
            .toolbar {
                flex-direction: column;
                align-items: stretch;
            }

            .search-container {
                max-width: none;
            }

            .action-buttons {
                flex-direction: row;
                gap: 0.25rem;
                justify-content: center;
            }

            .btn-sm {
                min-width: 60px;
                font-size: 0.8rem;
                padding: 0.25rem 0.5rem;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="container">
            <div class="title">
                <i class="bi bi-book"></i> 业务知识管理配置
            </div>
            <div class="subtitle">管理企业知识引擎，配置业务术语、黑话和常用表达</div>
            <div class="nav-links">
                <a href="/nl2sql.html" class="nav-link">
                    <i class="bi bi-house"></i> 首页
                </a>
                <a href="/business-knowledge.html" class="nav-link active">
                    <i class="bi bi-book"></i> 业务知识管理
                </a>
                <a href="/semantic-model.html" class="nav-link">
                    <i class="bi bi-diagram-3"></i> 语义模型配置
                </a>
            </div>
        </div>
    </div>

    <div class="container">
        <div class="toolbar">
            <div class="search-container">
                <input type="text" id="searchInput" class="search-input" placeholder="搜索业务名词、说明或同义词...">
                <button id="searchBtn" class="btn btn-primary">
                    <i class="bi bi-search"></i> 搜索
                </button>
            </div>
            <button id="addBtn" class="btn btn-success">
                <i class="bi bi-plus-circle"></i> 新增知识
            </button>
        </div>

        <div class="card">
            <div class="table-container">
                <table class="table">
                    <thead>
                        <tr>
                            <th>业务名词</th>
                            <th>说明</th>
                            <th>同义词</th>
                            <th>默认召回</th>
                            <th>数据集ID</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="tableBody">
                        <tr>
                            <td colspan="7" class="loading">
                                <div class="spinner"></div>
                                加载中...
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- 新增/编辑模态框 -->
    <div id="editModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title" id="modalTitle">新增业务知识</h3>
                <button class="close-btn" onclick="closeModal()">&times;</button>
            </div>
            <div class="modal-body">
                <form id="editForm">
                    <input type="hidden" id="editId">
                    <div class="form-group">
                        <label class="form-label" for="businessTerm">业务名词 *</label>
                        <input type="text" id="businessTerm" class="form-control" required 
                               placeholder="如：年龄分布、搜索业绩口径">
                    </div>
                    <div class="form-group">
                        <label class="form-label" for="description">说明 *</label>
                        <textarea id="description" class="form-control" rows="3" required 
                                  placeholder="输入对知识的定义、解释说明等"></textarea>
                    </div>
                    <div class="form-group">
                        <label class="form-label" for="synonyms">同义词</label>
                        <input type="text" id="synonyms" class="form-control" 
                               placeholder="多个同义词用逗号分隔，如：年龄画像,年龄构成,年龄结构">
                    </div>
                    <div class="form-group">
                        <label class="form-label" for="datasetId">数据集ID</label>
                        <input type="text" id="datasetId" class="form-control" 
                               placeholder="关联的数据集ID">
                    </div>
                    <div class="form-group">
                        <div class="checkbox-group">
                            <input type="checkbox" id="defaultRecall" class="form-control">
                            <label class="form-label" for="defaultRecall">默认召回</label>
                        </div>
                        <small style="color: #666; margin-left: 1.5rem;">勾选后，该知识每次提问时都会作为提示词传输给大模型</small>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" onclick="closeModal()">取消</button>
                <button type="button" class="btn btn-primary" onclick="saveKnowledge()">保存</button>
            </div>
        </div>
    </div>

    <script>
        let knowledgeList = [];
        let currentEditId = null;

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            loadKnowledgeList();
            
            // 绑定事件
            document.getElementById('searchBtn').addEventListener('click', searchKnowledge);
            document.getElementById('searchInput').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    searchKnowledge();
                }
            });
            document.getElementById('addBtn').addEventListener('click', showAddModal);
        });

        // 加载知识列表
        function loadKnowledgeList(keyword = '') {
            const url = keyword ? `/api/business-knowledge?keyword=${encodeURIComponent(keyword)}` : '/api/business-knowledge';
            
            fetch(url)
                .then(response => response.json())
                .then(data => {
                    knowledgeList = data;
                    renderTable();
                })
                .catch(error => {
                    console.error('加载数据失败:', error);
                    document.getElementById('tableBody').innerHTML = `
                        <tr>
                            <td colspan="7" class="empty-state">
                                <div class="empty-icon"><i class="bi bi-exclamation-triangle"></i></div>
                                <div>加载数据失败，请刷新页面重试</div>
                            </td>
                        </tr>
                    `;
                });
        }

        // 渲染表格
        function renderTable() {
            const tbody = document.getElementById('tableBody');
            
            if (knowledgeList.length === 0) {
                tbody.innerHTML = `
                    <tr>
                        <td colspan="7" class="empty-state">
                            <div class="empty-icon"><i class="bi bi-inbox"></i></div>
                            <div>暂无数据</div>
                        </td>
                    </tr>
                `;
                return;
            }

            tbody.innerHTML = knowledgeList.map(item => `
                <tr>
                    <td><strong>${item.businessTerm}</strong></td>
                    <td style="max-width: 300px; word-wrap: break-word;">${item.description}</td>
                    <td>${item.synonyms || '-'}</td>
                    <td>
                        <span class="badge ${item.defaultRecall ? 'badge-success' : 'badge-secondary'}">
                            ${item.defaultRecall ? '是' : '否'}
                        </span>
                    </td>
                    <td>${item.datasetId || '-'}</td>
                    <td>${formatDateTime(item.createTime)}</td>
                    <td>
                        <div class="action-buttons">
                            <button class="btn btn-primary btn-sm" onclick="showEditModal(${item.id})">
                                <i class="bi bi-pencil"></i> 编辑
                            </button>
                            <button class="btn btn-danger btn-sm" onclick="deleteKnowledge(${item.id})">
                                <i class="bi bi-trash"></i> 删除
                            </button>
                        </div>
                    </td>
                </tr>
            `).join('');
        }

        // 搜索知识
        function searchKnowledge() {
            const keyword = document.getElementById('searchInput').value.trim();
            loadKnowledgeList(keyword);
        }

        // 显示新增模态框
        function showAddModal() {
            currentEditId = null;
            document.getElementById('modalTitle').textContent = '新增业务知识';
            document.getElementById('editForm').reset();
            document.getElementById('editId').value = '';
            document.getElementById('editModal').classList.add('show');
        }

        // 显示编辑模态框
        function showEditModal(id) {
            const item = knowledgeList.find(k => k.id === id);
            if (!item) return;

            currentEditId = id;
            document.getElementById('modalTitle').textContent = '编辑业务知识';
            document.getElementById('editId').value = id;
            document.getElementById('businessTerm').value = item.businessTerm;
            document.getElementById('description').value = item.description;
            document.getElementById('synonyms').value = item.synonyms || '';
            document.getElementById('datasetId').value = item.datasetId || '';
            document.getElementById('defaultRecall').checked = item.defaultRecall;
            document.getElementById('editModal').classList.add('show');
        }

        // 关闭模态框
        function closeModal() {
            document.getElementById('editModal').classList.remove('show');
        }

        // 保存知识
        function saveKnowledge() {
            const form = document.getElementById('editForm');
            if (!form.checkValidity()) {
                form.reportValidity();
                return;
            }

            const data = {
                businessTerm: document.getElementById('businessTerm').value.trim(),
                description: document.getElementById('description').value.trim(),
                synonyms: document.getElementById('synonyms').value.trim() || null,
                datasetId: document.getElementById('datasetId').value.trim() || null,
                defaultRecall: document.getElementById('defaultRecall').checked
            };

            const url = currentEditId ? `/api/business-knowledge/${currentEditId}` : '/api/business-knowledge';
            const method = currentEditId ? 'PUT' : 'POST';

            fetch(url, {
                method: method,
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            })
            .then(response => response.json())
            .then(() => {
                closeModal();
                loadKnowledgeList();
            })
            .catch(error => {
                console.error('保存失败:', error);
                alert('保存失败，请重试');
            });
        }

        // 删除知识
        function deleteKnowledge(id) {
            if (!confirm('确定要删除这条业务知识吗？')) {
                return;
            }

            fetch(`/api/business-knowledge/${id}`, {
                method: 'DELETE'
            })
            .then(() => {
                loadKnowledgeList();
            })
            .catch(error => {
                console.error('删除失败:', error);
                alert('删除失败，请重试');
            });
        }

        // 格式化日期时间
        function formatDateTime(dateTimeStr) {
            if (!dateTimeStr) return '-';
            const date = new Date(dateTimeStr);
            return date.toLocaleString('zh-CN');
        }

        // 点击模态框外部关闭
        document.getElementById('editModal').addEventListener('click', function(e) {
            if (e.target === this) {
                closeModal();
            }
        });
    </script>
</body>
</html>